#include "server.h"

struct cnfg_info cnfg_info;
struct stat_info stat_info;

extern int worker_start();

static int _cnfg(char *argv0)
{/*{{{*/
    char dirname[LONG_FILE_NM_SIZE+1];
    char buf[LONG_FILE_NM_SIZE+1];
    int res;
    CNFG cnfg;
    struct cnfg_property property;

    extract_dirname(argv0, dirname);

    /*----- cnfg -----*/
    sprintf(buf, "%s/server.cnfg", dirname);
    if((res=cnfg_load(buf, &cnfg)))
    {
        printf("%s: load server.cnfg fail, errcode:%d\n",
                __func__, res);
        if(res > 0)
            printf("%s: line %d error\n", __func__, res);
        return -1;
    }

    /*--- ERRLOG ---*/
#ifdef _MY_DEBUG
    CLR_BUF(cnfg_info._errlog_.location);
    init_errlog(argv0, NULL);
#else
    strcpy(cnfg_info._errlog_.location, "./_log");
    if((res=cnfg_get_property(cnfg, "ERRLOG", "LOCATION", 0, &property)))
    {
        if(res != RET_ERR_CNFG_PROPERTY_NOT_FOUND)
        {
            printf("%s: get ERRLOG.LOCATION fail, res:%d\n",
                    __func__, res);
            return(-1);
        }
    }
    else
    {
        if(!BUF_IS_CLR(property.value))
            strcpy(cnfg_info._errlog_.location, property.value);
    }
    if((res=init_errlog(argv0, cnfg_info._errlog_.location)))
    {
        printf("%s: init_errlog fail, res:%d\n", __func__, res);
        return(-1);
    }
#endif
    if((res=cnfg_get_property(cnfg, "ERRLOG", "TRACKING", 0, &property)))
    {
        if(res != RET_ERR_CNFG_PROPERTY_NOT_FOUND)
        {
            errlog("%s: get ERRLOG.TRACKING fail, res:%d\n",
                    __func__, res);
            return(-1);
        }
        cnfg_info._errlog_.tracking=IS_FALSE;
    }
    else
        cnfg_info._errlog_.tracking=IS_TRUE;

    /*--- ACCESS ---*/
    // SERVER_PORT
    if((res=cnfg_get_property(cnfg, "ACCESS", "SERVER_PORT", 0, &property)))
    {
        errlog("%s: get ACCESS.SERVER_PORT fail, errcode:%d",
                __func__, res);
        return(-1);
    }
    if(BUF_IS_CLR(property.value) || (res=atoi(property.value)) < 1024)
    {
        errlog("%s: ACCESS.SERVER_PORT invalid", __func__);
        return(-1);
    }
    cnfg_info._access_.server_port=res;

    // MONITOR_PORT
    if((res=cnfg_get_property(cnfg, "ACCESS", "MONITOR_PORT", 0, &property)))
    {
        errlog("%s: get ACCESS.MONITOR_PORT fail, errcode:%d",
                __func__, res);
        return(-1);
    }
    if(BUF_IS_CLR(property.value) || (res=atoi(property.value)) < 1024)
    {
        errlog("%s: ACCESS.MONITOR_PORT invalid", __func__);
        return -1;
    }
    cnfg_info._access_.monitor_port=res;

    /*--- DB ---*/
    // LOCATION
    if((res=cnfg_get_property(cnfg, "DB", "LOCATION", 0, &property)))
    {
        errlog("%s: get DB.LOCALTION fail, errcode:%d",
                __func__, res);
        return(-1);
    }
    if(BUF_IS_CLR(property.value))
    {
        errlog("%s: DB.LOCALTION invalid", __func__);
        return(-1);
    }
    strcpy(cnfg_info._db_.location, property.value);

    cnfg_free(cnfg);

    return(0);
}/*}}}*/

static int _start()
{/*{{{*/
    int res;

    sig_ignore(SIGPIPE);
    ep_init();
    sqlic_init();

    res=sqlic_connect(cnfg_info._db_.location, &cnfg_info._db_.db);
    if(res != SQLI_OK)
    {
        errlog("%s: connect to %s fail, res:%d",
                __func__, cnfg_info._db_.location, res);
        return(-1);
    }

    return(worker_start());
}/*}}}*/

int main(int argc, char *argv[])
{
#ifndef _MY_DEBUG
    daemon(1, 1);
#endif
    if(_cnfg(argv[0]))
        return(-1);
    return(_start());
}
